﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace XNASpelProjekt
{
    /// <summary>
    /// Base class for all levels
    /// </summary>
    public abstract class Map
    {
        #region member variables
        protected Song music;
        protected Rectangle[] mapCollision;
        protected Rectangle[] mapDeathZone;
        protected Texture2D backgroundImage;

        public PlayerActor player;
        public List<EnemyActor> enemies;
        public List<EnemyActor> enemiesToRemove;
        public List<BulletActor> bullets;
        public List<BulletActor> bulletsToRemove;
        public List<AnimationActor> animations;
        public List<AnimationActor> animationsToRemove;
        
        public int GravityStrength { get; set; }

        #endregion

        #region public methods

        /// <summary>
        /// Updates the map, including all actors on the map
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public virtual void Update(GameTime gameTime)
        {
            //check if this map still is the current map
            if (Game1.map == this)
            {
                #region Update Player
                player.Update(gameTime);
                #endregion
            }
            if (Game1.map == this)
            {
                #region Update Enemies
                foreach (EnemyActor e in enemies)
                {
                    e.Update(gameTime);
                }

                foreach (EnemyActor e in enemiesToRemove)
                {
                    enemies.Remove(e);
                }
                enemiesToRemove.Clear();
                #endregion
            }
            if (Game1.map == this)
            {
                #region Update Bullets
                foreach (BulletActor b in bullets)
                {
                    b.Update(gameTime);
                }

                foreach (BulletActor b in bulletsToRemove)
                {
                    bullets.Remove(b);
                }
                bulletsToRemove.Clear();
                #endregion
            }
            if(Game1.map == this)
            {
                #region Update Animations
                foreach (AnimationActor a in animations)
                {
                    a.Update(gameTime);
                }

                foreach (AnimationActor a in animationsToRemove)
                {
                    animations.Remove(a);
                }
                animationsToRemove.Clear();
                #endregion
            }
        }

        /// <summary>
        /// Draws the map, including all actors on the map
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// <param name="spriteBatch">A SpriteBatch is used to draw textures.</param>
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //Vector2 for specifying drawing position
            Vector2 drawPos = new Vector2(0, 0);

            //draw background            
            spriteBatch.Draw(backgroundImage, drawPos, Color.White);

            //draw player
            player.Draw(gameTime, spriteBatch);

            //draw enemies
            foreach (EnemyActor e in enemies)
            {
                e.Draw(gameTime, spriteBatch);
            }

            //draw bullets
            foreach (BulletActor b in bullets)
            {
                b.Draw(gameTime, spriteBatch);

            }

            //draw animations
            foreach (Actor a in animations)
            {
                a.Draw(gameTime, spriteBatch);
            }

            //draw health
            drawPos.X = 50;
            drawPos.Y = 50;
            spriteBatch.DrawString(Game1.font, player.Health.ToString(), drawPos, Color.White);
        }

        /// <summary>
        /// Returns an array of the map's collision rectangles
        /// </summary>
        /// <returns>An array containing the map's collision rectangles</returns>
        public Rectangle[] getMapCollision()
        {
            return mapCollision;
        }

        /// <summary>
        /// Returns an array of the map's death zones.
        /// E.g water, spikes, lava, etc.
        /// </summary>
        /// <returns>An array containing the map's death zones</returns>
        public Rectangle[] getMapDeathZone()
        {
            return mapDeathZone;
        }

        #endregion
               
        #region constructors
        protected Map() { }

        #endregion

    }
}
